﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections.Generic;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Pdf
{
    public class TableOfContentModule : BaseModule
    {
        private PdfPTable tableToc;

        public TableOfContentModule(string title, IMetaProvider noProvider, Document pdfDocument, PdfFonts fontsProvider, BookConfiguration cfg)
            : base(noProvider, pdfDocument, fontsProvider, cfg)
        {
            // Title
            Tables.Add(new PdfBookPTable(TableTitle(title)));
 
            // Table for TOC informations
            tableToc = new PdfPTable(2);
            float[] widths = new float[] { 80f, 20f };
            tableToc.SetWidths(widths);
            tableToc.TotalWidth = TotalWidth;
            Tables.Add(new PdfBookPTable(tableToc));
        }


        public void SetTocContent(IEnumerable<TableOfContentsEntry> entries)
        {
            foreach (TableOfContentsEntry content in entries)
            {
                Phrase ph = new Phrase(content.Title);
                PdfPCell nameCell = new PdfPCell(tableToc);
                nameCell.Border = Rectangle.NO_BORDER;
                nameCell.PaddingLeft = 60f;
                nameCell.Phrase = new Phrase(content.Title, this.BookFonts.FontLevelTwo);
                tableToc.AddCell(nameCell);

                ph = new Phrase(content.Page, this.BookFonts.FontLevelTwo);
                Anchor click = new Anchor(ph);
                click.Reference = "#" + content.Title;

                PdfPCell pageCell = new PdfPCell(click);
                pageCell.Border = Rectangle.NO_BORDER;
                pageCell.Padding = 6f;

                tableToc.AddCell(pageCell);
            }

        }
    }
}
